package cache

import (
	"golang/cache/util"
	"time"
)

type Item struct {
	key          string
	value        interface{}
	expiryAt     int64
	neverExpires bool // default false
}

func newItem(key string, value interface{}, duration time.Duration, mode Mode) *Item {
	it := &Item{
		key:      key,
		value:    value,
		expiryAt: time.Now().Add(duration).Unix(),
	}
	if duration == 0 {
		it.neverExpires = true
	}
	if mode == ReadOnly {
		_ = util.DeepCopy(value, it.value)
	}
	return it
}

func (item *Item) isExpired() bool {
	if item.neverExpires {
		return false
	}
	now := time.Now().Unix()
	return item.expiryAt <= now
}
